package scavenger.client;


//import java.io.File;
//import java.io.FileOutputStream;
//import java.io.IOException;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Text;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import android.util.Log;


////For jdk1.5 with built in xerces parser
//import com.sun.org.apache.xml.internal.serialize.OutputFormat;
//import com.sun.org.apache.xml.internal.serialize.XMLSerializer;


//Base imports
import java.net.*;
import java.io.*;

public class GameClient {

    private GameState gamestate;
    private String host;
    private int outputPort;
    private int inputPort;

    GameClient(String host, int outputPort, int inputPort){
		this.gamestate = null;
		this.host = host;
		this.outputPort = outputPort;
		this.inputPort = inputPort;
    }

    public void submitAction(Action action){
		try {
	
		    /**
		     * Create GameClient/GameServer Connections 
		     */
	
		    /** Obtain an address object of the server */
		    InetAddress address = InetAddress.getByName(host);
	
		    /** Establish a socket connection */
		    Socket sout = new Socket(address, outputPort);
		    Socket sin = new Socket(address, inputPort);
	
		    /** Instantiate an OutputStream object */
		    OutputStream os = sout.getOutputStream();
		    InputStream is = sin.getInputStream();
	
		    /**
		     *
		     * Send Action to GameServer
		     *
		     */
	
		    /** Create Action DOM from Action Object */
		    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
		    DocumentBuilder db = dbf.newDocumentBuilder();
		    Document dom = db.newDocument();
		    Element rootEle = action.toXmlElement(dom);                       
		    dom.appendChild(rootEle);                                           
	
		    /** Set XML Formatting */
		    //OutputFormat format = new OutputFormat(dom);
		    //format.setIndenting(true);
	
		    /** Transmit Serialized XML from Action DOM */
		    //XMLSerializer serializer = new XMLSerializer(os, format);
		    //serializer.serialize(dom);
		    //os.close();
	
		    
		    TransformerFactory factory = TransformerFactory.newInstance();
		    Transformer transformer = factory.newTransformer();
		    
		    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
		    transformer.setOutputProperty(OutputKeys.METHOD, "xml");
		    
		    DOMSource domSource = new DOMSource(dom);
		    StreamResult result = new StreamResult(os);
	
		    transformer.transform(domSource, result);
		    os.close();
		    
		    /**
		     *
		     * Receive Updated GameState From GameServer
		     *
		     */
	
		    /** Create document object model from XML parse */
		    dbf = DocumentBuilderFactory.newInstance();
		    db = dbf.newDocumentBuilder();
		    dom = db.parse(is);
		    is.close();
	
		    /** Create GameState object from DOM parse */
		    gamestate = new GameState(dom.getDocumentElement());
		}
		catch(ParserConfigurationException pce) {
		    pce.printStackTrace();
		}
		catch(SAXException se) {
		    se.printStackTrace();
		}
		catch(IOException ioe) {
		    ioe.printStackTrace();
		}
		catch (Exception e) {
			android.util.Log.v("test", "Error = "+e.getMessage());
		}

    }

    public GameState getGameState(){
	return(gamestate);
    }

    /*public static void main(String[] args) {
	
	GameClient gc = new GameClient("localhost",19998,19999);

	gc.submitAction(new Action());

	GameState gs = gc.getGameState();

	System.out.println("gamestate: " + gs.getValue());

    }*/

}




